<template>
  <div class="app-container">
    <div style="text-align: center;margin-bottom: 30px">
      异常信息更新
    </div>
    <el-form :model="abnormalFrom"  ref="abnormalFrom" :rules="abnormalFromRules" label-width="100px">
      <el-row :gutter="10">
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="异常名称" prop="name">
              <el-input type="text" v-model="abnormalFrom.name" placeholder="请输入异常名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="申报人" prop="abnormalUser">
                <el-select v-model="abnormalFrom.abnormalUser" placeholder="请选择申报人"
                           style="width: 100%">
                    <el-option v-for="(item,index) in userList" :key="index"  :value="item.ucId" :label="item.name" ></el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="异常描述" prop="remark">
              <el-input type="text" v-model="abnormalFrom.remark" placeholder="请输入异常描述" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="所属主体" prop="enterpriseId" v-if="enterPriseHidden">
                    <el-select v-model="abnormalFrom.enterpriseId" placeholder="请选择主体" style="width: 100%" clearable @clear="clearInput1()">
                        <el-option v-for="(item,index) in enterpriseDominantList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                    </el-select>
            </el-form-item>
          </el-col>
            <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                <el-form-item label="异常时间" prop="abnormalTime">
                        <el-date-picker
                                v-model="abnormalFrom.abnormalTime"
                                type="datetime"
                                placeholder="选择日期"
                        style="width: 100%">
                        </el-date-picker>
                </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                <el-form-item label="异常状态" prop="abnormalStatus">
                    <el-select v-model="abnormalFrom.abnormalStatus" placeholder="请选择异常状态" style="width: 100%">
                        <el-option :value="0" label="异常"></el-option>
                        <el-option :value="1" label="正常"></el-option>
                    </el-select>
                </el-form-item>
            </el-col>
        </el-col>
      </el-row>
    </el-form>

    <div slot="footer" style="text-align: center" class="dialog-footer">
      <el-button @click="cancelabnormalFrom">取 消</el-button>
      <el-button :loading="addLoading"  type="primary" @click="updateabnormalFrom">修 改</el-button>
    </div>
  </div>
</template>

<script>
import {addInAbnormal, getInAbnormalVo, updateInAbnormal} from "@/api/abnormal";
import {getInEnterpriseList, getInUserList} from "@/api/enterprise";
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";

export default {
  name: "InabnormalFromEdit",
  data() {
    return {
      abnormalFrom: {

      },
      abnormalFromRules: {
        name: [{required: true, message: '异常名称不能为空', trigger: 'blur'}],
        remark: [{required: true, message: '异常详情不能为空', trigger: 'blur'}],
        abnormalFromUser: [{required: true, message: '申报人不能为空', trigger: 'blur'}],
        enterpriseId: [{required: true, message: '企业不能为空', trigger: 'blur'}],
      },
      addLoading: false,
      imgFlag: false,
      imgUploadPercent: 0,
        enterpriseDominantList:[],
        userList:[],
    }
  },
  computed:{
    enterPriseHidden(){
      let roleCode = this.$store.getters.roleCode;
      return roleCode=="ADMIN"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"
    }
  },
  created() {
      this.getInEnterpriseDominantList()
      this.getInUserList()
      let ucId = this.$route.params.ucId
      console.log(ucId)
      this.getInabnormalFromVo(ucId)

  },
  methods: {
    getInabnormalFromVo(val) {
        getInAbnormalVo({ucId: val}).then(res => {
        this.abnormalFrom = res.data
      }).catch(res => {

      })
    },
    cancelabnormalFrom() {
      window.history.back()
    },
    addabnormalFrom() {
      this.addLoading = true
      this.$refs['abnormalFrom'].validate(valid => {
        if (valid) {
            addInAbnormal(this.abnormalFrom).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "添加异常信息成功"
            })
            this.cancelabnormalFrom()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    updateabnormalFrom() {
      this.addLoading = true
      this.$refs['abnormalFrom'].validate(valid => {
        if (valid) {
            updateInAbnormal(this.abnormalFrom).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "更新异常信息成功"
            })
            this.cancelabnormalFrom()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
      getInEnterpriseDominantList(){
          getInEnterpriseDominantList({deleted:false}).then(res=>{
              this.enterpriseDominantList = res.data
          }).catch(error=>{

          })
      },
      getInUserList(){
          getInUserList({deleted:false}).then(res=>{
              this.userList = res.data
          }).catch(error=>{
              alert("获取申报人列表失败")
          })
      },
  }
}

</script>



